/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sessionBeans;

import entityBeans.ReplyEntity;
import entityBeans.SubjectEntity;
import entityBeans.ThreadEntity;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import utilities.HashFunctions;

/**
 *
 * @author h07029
 */
@Stateless
public class ThreadSessionBean implements ThreadSessionBeanLocal {
    
    @PersistenceContext(unitName = "forum-ejbPU")
    private EntityManager em;

    @Override
    public List<ReplyEntity> getMyReply(ThreadEntity myThreadEntity) {
        
        javax.persistence.Query q = em.createNamedQuery("ThreadEntity.getMyReplies");
        q.setParameter("myThread",myThreadEntity);
        
        List<ReplyEntity> listR = q.getResultList();
        return (!listR.isEmpty()) ? listR : null;
}

    @Override
    public List<ThreadEntity> getThreadBySubject(SubjectEntity mySubject) {
        Query q = em.createNamedQuery("ThreadEntity.getThreadBySubject");
        q.setParameter("mySubject", mySubject);        
        return q.getResultList();
    }

    @Override
    public List<ReplyEntity> getMyReplies(ThreadEntity currentThread) {
        Query q = em.createNamedQuery("ThreadEntity.getMyReplies");
        q.setParameter("myThread", currentThread);
        return q.getResultList();
    }
    
    @Override
    public ThreadEntity getThreadByUuid(String myUuid) {
        Query q = em.createNamedQuery("ThreadEntity.threadByUuid").setParameter("myUuid",myUuid);
        return (q.getResultList().size() > 0) ? (ThreadEntity) q.getResultList().get(0) : null;
    }
    
    @Override
    public ThreadEntity addThread(SubjectEntity mySubject,ThreadEntity newThread) {
        newThread.setMySubject(mySubject);
        em.persist(newThread);
        newThread.setUuid(HashFunctions.stringToSHA1(newThread.getId().toString()));
        mySubject.getMyThreads().add(newThread);
        em.merge(mySubject);
        return newThread;
    }
    
    @Override
    public ReplyEntity addNewPost(ThreadEntity myThread, String object, String textValue) {
        Date newDate;
        newDate = new Date();
        ReplyEntity newReply = new ReplyEntity();
        newReply.setMyThread(myThread);
        newReply.setMyDate(newDate);
        
        newReply.setContent(textValue);
        newReply.setMessageObject(object);
        
        em.persist(newReply); //Persistance de le nouvelle réponse
        myThread.addReply(newReply); //Ajout de la réponse au thread
        
        em.merge(myThread); //Merging de la conversation
        
        return newReply;
    }
    
    @Override
    public ReplyEntity addReply(ThreadEntity newThread,ReplyEntity myReply) {
        myReply.setMyThread(newThread);
        em.persist(myReply);
        newThread.getMyReplies().add(myReply);
        em.merge(newThread);
        return myReply;
    }
}
